package com.hainatong.shiphelper.module.warehouse.viewmodel;


import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.api.*;
import com.hainatong.shiphelper.mainhome.entity.UploadImgBean;
import com.hainatong.shiphelper.mainhome.entity.WhereComBean;
import com.hainatong.shiphelper.module.warehouse.entity.BrokenParamBean;
import com.hainatong.shiphelper.module.warehouse.entity.ImpaBean;
import com.hainatong.shiphelper.module.warehouse.entity.SupplierBean;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrokenViewModel extends BaseViewModel
{

    public BrokenViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<LiveResult<Object>> brokenResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Object>> getBrokenResult()
    {
        return brokenResult;
    }

    /**
     *   破损 提交
     * @param brokenparam
     */
    public void submitBrokenParam(BrokenParamBean brokenparam)
    {
        ApiService.Builder.getChanBoServer()
                          .commonBroken( brokenparam )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<Object>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                  addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getBrokenResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<Object> result)
                              {
                                  //{"data":[{"children":[{"uuid":"20e3c954-e86c-4e75-b7ff-f054f561b92f","code":"NO6-002"}],"shelfName":"1号货架","floors":"2"}],"status":true,"code":"200","message":"操作成功"}
                                  if( result.isOk() )
                                  {
                                      getBrokenResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getBrokenResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                                  }
                              }
                          });

    }

    private MutableLiveData<LiveResult<ImpaBean>> impaResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<ImpaBean>> getImpaResult()
    {
        return impaResult;
    }

    /**
     * 查 物资
     * @param code
     */
    public void queryImpaByCode(String code)
    {
        if( code.length() > 35 )
        {
            return;
        }
        ApiService.Builder.getChanBoServer()
                .getImpaByCode(code)
                .compose(RxSchedulersUtils.Obs_io_main())
                .subscribe(new VMObserver<ApiResult<ImpaBean>>() {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        getImpaResult().postValue( new LiveResult<>( false ,errorMsg ) );
                    }

                    @Override
                    public void onSuccess(ApiResult<ImpaBean> result)
                    {
                        if( result.isOk() )
                        {
                            getImpaResult().postValue( new LiveResult<>( true , result.getData() ) );
                        }
                        else
                        {
                            getImpaResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                        }
                    }
                });

    }


    private MutableLiveData<LiveResult<UploadImgBean>> upImgResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<UploadImgBean>> getUpImgResult()
    {
        return upImgResult;
    }

    /**
     *  上传 单张 图片
     * @param imgFile
     */
    public void upLoadImg(File imgFile)
    {

        RequestBody imgBody = RequestBody.create(MediaType.parse("image/*"), imgFile);
        MultipartBody.Part  body = MultipartBody.Part.createFormData( "file",imgFile.getName() , imgBody );

        ApiService.Builder.getChanBoServer()
                          .uploadImgSingle(body)
                          .compose(RxSchedulersUtils.Obs_io_main())
                          .subscribe(new VMObserver<ApiResult<UploadImgBean>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                 addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getUpImgResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<UploadImgBean> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getUpImgResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getUpImgResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                                  }
                              }
                          });
    }


    private MutableLiveData<LiveResult<List<SupplierBean>>> supplierResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<SupplierBean>>> getSupplierResult()
    {
        return supplierResult;
    }

    public void querySupplier()
    {
        Map<String,Object> param = new HashMap<String,Object>();
        param.put(Constants.Key.PAGE, "1");
        param.put( Constants.Key.ROWS, "100");//Constants.DEF_PAGE_SIZE

        List<WhereComBean> whereList = new ArrayList<>();
        whereList.add( new WhereComBean( "is_deleted", "0" ) );
        param.put( "wheres", GsonUtil.obj2json( whereList ));
        ApiService.Builder.getChanBoServer()
                          .getSupplierList(param)
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<HttpResult<List<SupplierBean>>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getSupplierResult().postValue( new LiveResult<>( false,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(HttpResult<List<SupplierBean>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getSupplierResult().postValue( new LiveResult<>( true, result.getRows()  ) );
                                  }
                                  else
                                  {
                                      getSupplierResult().postValue( new LiveResult<>( false,result.getMsg() ) );
                                  }
                              }
                          });
    }

}
